package com.zjq.springsocial.renren.api.impl.json;

import java.io.IOException;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.annotate.JsonDeserialize;

import com.zjq.springsocial.renren.api.ImageSize;

@JsonIgnoreProperties(ignoreUnknown = true)
abstract class ImageMixin {
	
	@JsonCreator
	ImageMixin(
			@JsonProperty("url") String url,
			@JsonProperty("size") @JsonDeserialize(using = ImageSizeDeserializer.class)
			ImageSize size
	){}
	
	private static class ImageSizeDeserializer extends JsonDeserializer<ImageSize> {
		@Override
		public ImageSize deserialize(JsonParser jp, DeserializationContext ctxt)
				throws IOException, JsonProcessingException {
			try {
				return ImageSize.valueOf(jp.getText().toUpperCase());
			} catch (IllegalArgumentException e) {
				return ImageSize.MAIN;
			}
		}
	}
}
